package com.ccp.dev.qbdms.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.DateUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.model.WQbdFalProblemreset;
import com.ccp.dev.qbdms.service.AfterSaleQualityProbleService;
import com.ccp.dev.qbdms.service.WQbdFalProblemresetService;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProcessingTimeUtil;
import com.ccp.dev.system.service.SysOrgService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zzy on 2020/7/20.
 *
 * @Author zzy
 * @Date 2020/7/20
 * @describe 售后质量问题统计
 */
@Controller
@Action(customModel = "售后质量问题统计")
@RequestMapping("/qbdms/aftersalequalityproble")
public class AfterSaleQualityProbleController extends SysBaseController {

    @Resource
    private AfterSaleQualityProbleService afterSaleQualityProbleService;
    @Resource
    private WQbdFalProblemresetService wQbdFalProblemresetService;
    @Resource
    private SysOrgService sysOrgService;
    @Resource
    private ScriptImpl scriptimpl;
    /**统计图 饼图*/
    private final String CHART_PIE = "pie";
    /**统计图 柱图*/
    private final String CHART_BAR = "bar";
    /**工作阶段 交付使用*/
    private final String FWORKPHASE1_JFXY = "交付使用";
    /**问题类型 售后*/
    private final String SH_TYPE_SH = "售后问题";
    /**问题类型 其他*/
    private final String SH_TYPE_QT = "其他问题";

    /**
     * 跳转页面
     *
     * @return
     */
    @GetMapping("/index")
    @Action(description = "跳转到售后质量问题统计页面", exectype = "页面跳转日志")
    public String getView(ModelMap modelMap) {
        modelMap.addAttribute("nowyear", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
        return "/aftersalequalityproble/index";
    }

    /**
     * 获取统计数据
     *
     * @param request
     * @return
     */
    @GetMapping("/getData")
    @ResponseBody
    @Action(description = "获取售后质量问题统计数据", detail = "获取售后质量问题统计数据<#if success>成功<#else>失败</#if>")
    public Object getData(HttpServletRequest request) {
        String start = RequestUtil.getString(request, "Q_beginCreateTime_DL");
        String end = RequestUtil.getString(request, "Q_endCreateTime_DG");
        if (StringUtil.isEmpty(start) || StringUtil.isEmpty(end)) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("日期参数出错");
        }
        Date sdate = DateUtil.parseDate(start);
        Date edate = DateUtil.parseDate(end);
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return success(afterSaleQualityProbleService.getChartData(sdate, edate));
    }

    /**
     * 获取用来填充表格的数据
     * @param request
     * @return
     */
    @GetMapping("/getTableData")
    @ResponseBody
    @Action(description = "获取售后质量问题统计对应的台账数据", detail = "获取售后质量问题统计对应的台账数据<#if success>成功<#else>失败</#if>")
    public Object getTableData(HttpServletRequest request){
        QueryFilter qf = new QueryFilter(request);
        qf.put("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
        qf.put("secret", scriptimpl.getCurrentUser().getPrivacyLevel());
        ZhiLiangConst.addReportCodeConditions(qf);

        String type = RequestUtil.getString(request, "tdtype");
        if (StringUtil.isEmpty(type)) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("参数异常!");
        }
        if (CHART_BAR.equals(type)) {
            //如果是饼图,则传入的参数是年份
            String parm = RequestUtil.getString(request, "tdparm");
            if (StringUtil.isEmpty(parm)) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return fail("参数异常");
            }
            Date datestr = DateUtil.parseDate(parm);
            Map<String, Object> datemap = ProcessingTimeUtil.converSimpleDateRange("andtj", datestr, datestr);
            qf.put("beginTime",datemap.get("startTime"));
            qf.put("endTime",datemap.get("endTime"));
            qf.put("fWorkphase1", FWORKPHASE1_JFXY);
        }
        else if(CHART_PIE.equals(type)){
            //如果饼图,传入的是年份和问题类型
            String probtype = RequestUtil.getString(request, "tdprobtype");
            String tdstart = RequestUtil.getString(request, "tdstart");
            String tdend = RequestUtil.getString(request, "tdend");
            if (StringUtil.isEmpty(probtype) || StringUtil.isEmpty(tdstart) || StringUtil.isEmpty(tdend)) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return fail("参数异常");
            }
            Date sdate = DateUtil.parseDate(tdstart);
            Date edate = DateUtil.parseDate(tdend);
            Map<String, Object> datemap = ProcessingTimeUtil.converSimpleDateRange("andtj", sdate, edate);
            qf.put("beginTime",datemap.get("startTime"));
            qf.put("endTime",datemap.get("endTime"));
            // 工作阶段为交付使用的
            if (SH_TYPE_SH.equals(probtype)) {
                qf.put("fWorkphase1", FWORKPHASE1_JFXY);
            }
            else{
                qf.put("fWorkphase1_sh_qt", FWORKPHASE1_JFXY);
            }
        }
        List<WQbdFalProblemreset> listdata = wQbdFalProblemresetService.queryList(qf);
        try {
            ZhiLiangConst.showName(listdata);
        } catch (IllegalAccessException e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            e.printStackTrace();
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return success(listdata, qf.getPageBean().getTotalCount());
    }

}
